/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.impl.restlet.resources;

import java.util.Collections;

import org.restlet.data.Method;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.ResourceException;

import com.semagia.atomico.MediaType;
import com.semagia.atomico.dm.IWritableRepresentation;
import com.semagia.atomico.server.dm.IFragmentInfo;
import com.semagia.atomico.server.storage.StorageException;

/**
 * Represents a fragment.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 * @version $Rev: 66 $ - $Date: 2010-09-06 15:29:07 +0000 (Mon, 06 Sep 2010) $
 */
public class FragmentResource extends AbstractBaseResource implements
        IVariantConstants {

    /**
     * The fragment info to operate on.
     */
    private IFragmentInfo _fragmentInfo;

    /**
     * Initializes the instance.
     */
    public FragmentResource() {
        super();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.restlet.resource.ServerResource#getPreferredVariant(org.restlet.data.Method)
     */
    @Override
    public Variant getPreferredVariant(Method method) {
        if (Method.GET.equals(method)) {
            return VARIANT_XTM;
        }
        return super.getPreferredVariant(method);
    }

    /* (non-Javadoc)
     * @see org.restlet.resource.UniformResource#doInit()
     */
    @Override
    protected void doInit() throws ResourceException {
        super.initializeCollectionInfo();
        try {
            final String fragmentId = (String) super.getRequest()
                    .getAttributes().get(FRAGMENT_ID);
            _fragmentInfo = fragmentId != null ? _storage.getFragmentInfo(
                    _collInfo.getCollectionId(), fragmentId) : null;
        }
        catch (StorageException ex) {
            throw new ResourceException(ex);
        }
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.server.impl.restlet.resources.AbstractBaseResource#isExists()
     */
    @Override
    public boolean isExisting() {
        return _fragmentInfo != null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.semagia.atomico.server.impl.restlet.resources.AbstractBaseResource#getMediaTypes()
     */
    @Override
    protected Iterable<MediaType> getMediaTypes() {
        return _fragmentInfo != null ? _fragmentInfo.getMediaTypes()
                : Collections.<MediaType> emptySet();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.semagia.atomico.server.impl.restlet.resources.AbstractBaseResource#lastModification()
     */
    @Override
    protected long lastModification() {
        return _fragmentInfo != null ? _fragmentInfo.getUpdated() : -1;
    }

    /* (non-Javadoc)
     * @see org.restlet.resource.ServerResource#get(org.restlet.representation.Variant)
     */
    @Override
    public Representation get(Variant variant) throws ResourceException {
        IWritableRepresentation tm = null;
        try {
            tm = _storage.getFragment(_collInfo.getCollectionId(),
                    _fragmentInfo.getFragmentId(), MediaTypeUtils
                            .toAtomicoMediaType(variant.getMediaType()));
        }
        catch (StorageException ex) {
            throw new ResourceException(ex);
        }
        return new WritableAwareRepresentation(variant.getMediaType(), tm,
                _fragmentInfo.getUpdated());
    }

}
